package formal.array;

import java.util.Arrays;

/**
 * @author DengYuan2
 * @create 2021-02-06 20:42
 */
public class E_645 {
    public static void main(String[] args) {
//        int[] nums = {1,2,2,4};
//        int[] nums = {2,3,2};
        int[] nums = {8, 7, 3, 5, 3, 6, 1, 4};
//        int[] nums = {2,2};
        int[] res = findErrorNums(nums);
        System.out.println(Arrays.toString(res));
    }

    /**
     * 我的思路：将数组上的数放到合适的位置上，但一直没有做到
     * 8 3 3 4 5 6 7 1
     *
     * @param nums
     * @return
     */
    public static int[] findErrorNums(int[] nums) {
        int[] res = new int[2];
        for (int i = 0; i < nums.length; i++) {
            while (nums[i] != (i + 1) && nums[i] != nums[nums[i] - 1]) {
                int tmp = nums[i];
                nums[i] = nums[tmp-1];
                nums[tmp-1]=tmp;
            }
        }

        for (int i = 0; i < nums.length; i++) {
            if (nums[i] != (i + 1)) {
                res[0] = nums[i];
                res[1] = i + 1;
                return res;
            }

        }
        return null;
    }
}
